﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Polly;

namespace NetMicro.HttpClient.Polly
{
    /// <summary>
    /// polly策略集合
    /// </summary>
    public static class PollyQueue
    {
        private static ConcurrentDictionary<string, IAsyncPolicy<HttpResponseMessage>> PollKeyValues = new ConcurrentDictionary<string, IAsyncPolicy<HttpResponseMessage>>();

        /// <summary>
        /// 添加策略集合缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="policy"></param>
        public static void Add(string key, IAsyncPolicy<HttpResponseMessage> policy)
        {
            if (!PollKeyValues.ContainsKey(key))
            {
                PollKeyValues.TryAdd(key, policy);
            }
        }
        /// <summary>
        /// 获取策略集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IAsyncPolicy<HttpResponseMessage> Get(string key)
        {
            PollKeyValues.TryGetValue(key, out IAsyncPolicy<HttpResponseMessage> value);
            return value;
        }

        /// <summary>
        /// 查询策略是否已存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool ContainsKey(string key)
        {
            return PollKeyValues.ContainsKey(key);
        }
    }
}
